<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 使用卡片组件实现外层容器 -->
      <el-card class="box-card">
        <!-- 使用 tabs 组件实现上层标签 -->
        <el-tabs v-model="activeName">
          <el-tab-pane class="tab-pane" label="组织架构" name="first">
            <!-- 树形结构头部 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <!-- 左侧标题 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">北京节流云科技股份有限公司</span>
              </el-col>
              <!-- 右侧操作 -->
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作
                        <i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDept('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 使用树形控件 实现整体结构 -->
            <el-tree
              class="departments-tree"
              default-expand-all
              :data="treeData"
              :props="defaultProps"
              @node-click="handleNodeClick"
            >
              <!-- 右侧 -->
              <template slot-scope="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  style="height: 50px; width: 100%"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <!-- 添加部门需要id -->
                            <el-dropdown-item
                              @click.native="addDept(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDept(data.id)"
                            >编辑部门</el-dropdown-item>
                            <!-- v-if 做判断 如果当前部门下有子部门 ,则删除按钮不显示 -->
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDept(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 添加或编辑弹框 -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 添加子组件弹层 -->
          <deptDialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :chu-list="chuList"
            @guanbi="guanBiFn"
            @quXiao="quXiaoFn"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 数据api
import { zuZhiJiaGouAPI, delBuMenAPI } from '@/api/departments'
// 处理树形数据
import { tranListToTreeData } from '@/utils'
// 添加和编辑弹窗
import deptDialog from './deptDialog'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      parentId: '', // 父级id
      activeName: 'first', // 最上层组织架构
      showDialog: false, // 添加和删除 弹窗 显示隐藏
      isEdit: false, // 是否是编辑 ..区分添加编辑弹窗
      chuList: [], // 表单校验数据
      // 树形控件数据
      treeData: [],
      // 树形定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },

  created() {
    // 调用封装的获取数据接口
    this.zuZhiJiaGou()
  },

  methods: {

    // 点击树形控件触发事件
    handleNodeClick(data) {
      console.log(data)
    },

    // 封装一个获取组织架构数据接口
    async zuZhiJiaGou() {
      const res = await zuZhiJiaGouAPI()

      // 做表单校验所需要的数据
      this.chuList = res.data.depts.map(item => ({
        id: item.id,
        code: item.code,
        pid: item.pid,
        name: item.name
      }))

      // treeData 接收数据
      // this.treeData = res.data.depts
      this.treeData = tranListToTreeData(res.data.depts, '')
    },

    // 添加弹窗显示 -------
    addDept(id) {
      this.parentId = id
      this.showDialog = true

      this.isEdit = false // 是否是编辑
    },

    // 添加成功后关闭弹窗 并 重新调用接口
    guanBiFn() {
      this.showDialog = false
      this.zuZhiJiaGou()
    },

    // 编辑弹窗 -----------
    editDept(id) {
      this.parentId = id // 也要当前id
      this.showDialog = true

      this.isEdit = true // 是否是编辑
    },

    // 删除部门
    async delDept(id) {
      // 点击删除先出现弹层
      const tanceng = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err) // 返回结果
      // console.log(tanceng) // 点击确定 打印confirm --------- 点击取消  打印cancel
      // 点击取消
      if (tanceng === 'cancel') {
        return this.$message({
          type: 'info',
          message: '已取消删除'
        })
      }
      // 发起删除请求
      const res = await delBuMenAPI(id)
      // console.log(res)
      // 判断有无异常
      if (!res.success) return this.$message.error(res.message)
      // 删除成功
      this.$message({
        type: 'success',
        message: '删除部门成功!'
      })
      this.zuZhiJiaGou()
    },

    // 取消按键
    quXiaoFn() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

// 树形标题
.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}

// 树形结构
.departments-tree {
  margin: 12px 0px;
}
</style>
